package top.lilele.adminSystem.web;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lilele.adminSystem.entity.AClothClass;
import top.lilele.adminSystem.service.EmailService;
import top.lilele.adminSystem.utils.AsyncOperate;
import top.lilele.adminSystem.utils.BaseResponse;
import top.lilele.adminSystem.utils.ResponseCodeEnum;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: li.leLe
 * @Date: 2021/8/27 09:19
 */
@RestController
@RequestMapping(value = "test")
public class TestController {

    @Resource
    AsyncOperate asyncOperate;

    @GetMapping(value = "a")
    private Boolean test() {
        return StpUtil.isLogin();
    }

    @Resource
    EmailService emailService;

    @GetMapping(value = "testTree")
    public List testTree() {
        List<AClothClass> lists = CollUtil.newArrayList();
        lists.add(new AClothClass(1, "顶级目录1", 0, 1));
        lists.add(new AClothClass(2, "顶级目录2", 0, 1));
        lists.add(new AClothClass(3, "顶级目录3", 0, 1));
        lists.add(new AClothClass(4, "二级目录4", 1, 1));
        lists.add(new AClothClass(5, "三级目录5", 4, 1));
        lists.add(new AClothClass(6, "四级目录6", 5, 1));
        lists.add(new AClothClass(7, "二级目录7", 2, 1));
        lists.add(new AClothClass(8, "二级目录8", 2, 1));
        lists.add(new AClothClass(9, "三级目录9", 4, 1));
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("pid");
        List<Tree<String>> treeNodes = TreeUtil.build(lists, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getPid().toString());
                    tree.setName(treeNode.getName());
                    tree.putExtra("status", treeNode.getStatus());
                });
        System.out.println((treeNodes));
        return treeNodes;
    }

    @GetMapping(value = "loginTest")
    public BaseResponse loginTest() {
//        StpUtil.checkPermission("user:edit:add");
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    @GetMapping(value = "sendEmail")
    public String sendEmail() {
//        emailService.sendMail();
//        asyncOperate.sendAddUserMail();
        HashMap<Object, Object> maps = Maps.newHashMap();
        maps.put("username", "xiaoming");
        maps.put("password", "123456");
        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
        Template template = engine.getTemplate("addUserEmail.ftl");
        String result = template.render(maps);
        return result;
    }
}
